package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyUserInfoAddBo;
import com.ruoyi.system.bo.ZsyyUserInfoQueryBo;
import com.ruoyi.system.bo.ZsyyUserInfoEditBo;
import com.ruoyi.system.domain.ZsyyUserInfo;
import com.ruoyi.system.mapper.ZsyyUserInfoMapper;
import com.ruoyi.system.vo.ZsyyUserInfoVo;
import com.ruoyi.system.service.IZsyyUserInfoService;
import com.ruoyi.system.mapper.ZsyyUserInfoMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
/**
 * 用户详细信息Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyUserInfoServiceImpl extends ServicePlusImpl<ZsyyUserInfoMapper, ZsyyUserInfo> implements IZsyyUserInfoService {
    @Autowired
    private ZsyyUserInfoMapper iZsyyUserInfoMapper;

    @Override
    public ZsyyUserInfoVo queryById(Long id){
        return getVoById(id, ZsyyUserInfoVo.class);
    }

    @Override
    public TableDataInfo<ZsyyUserInfoVo> queryPageList(ZsyyUserInfoQueryBo bo) {
        PagePlus<ZsyyUserInfo, ZsyyUserInfoVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyUserInfoVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyUserInfoVo> queryList(ZsyyUserInfoQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyUserInfoVo.class);
    }

    private LambdaQueryWrapper<ZsyyUserInfo> buildQueryWrapper(ZsyyUserInfoQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyUserInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ZsyyUserInfo::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerId()), ZsyyUserInfo::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getCustomerSeq() != null, ZsyyUserInfo::getCustomerSeq, bo.getCustomerSeq());
        lqw.like(StrUtil.isNotBlank(bo.getCustomerName()), ZsyyUserInfo::getCustomerName, bo.getCustomerName());
        lqw.eq(bo.getCustomerSex() != null, ZsyyUserInfo::getCustomerSex, bo.getCustomerSex());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerTelphone()), ZsyyUserInfo::getCustomerTelphone, bo.getCustomerTelphone());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerCompany()), ZsyyUserInfo::getCustomerCompany, bo.getCustomerCompany());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerDepartment()), ZsyyUserInfo::getCustomerDepartment, bo.getCustomerDepartment());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerAddress()), ZsyyUserInfo::getCustomerAddress, bo.getCustomerAddress());
        lqw.eq(StrUtil.isNotBlank(bo.getIdentityCardNumber()), ZsyyUserInfo::getIdentityCardNumber, bo.getIdentityCardNumber());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyUserInfoAddBo bo) {
        ZsyyUserInfo add = BeanUtil.toBean(bo, ZsyyUserInfo.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyUserInfoEditBo bo) {
        ZsyyUserInfo update = BeanUtil.toBean(bo, ZsyyUserInfo.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyUserInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
